package com.c;

import com.c.pojo.entity.Answer;
import com.c.pojo.model.Article;
import com.c.service.AnswerService;
import com.c.util.CheckUtil;
import com.c.util.MessageUtil;
import org.dom4j.DocumentException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * @author bysun
 */
@Controller
public class WxConf {

    @Resource
    AnswerService answerService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    @ResponseBody
    public void login(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("success");
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            if (CheckUtil.checkSignature(signature, timestamp, nonce)) {
                out.write(echostr);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    public void wx(HttpServletRequest request, HttpServletResponse response) throws IOException, DocumentException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String message = "";
        try {
            Map<String, String> map = MessageUtil.xmlToMap(request);
            String toUserName = map.get("ToUserName");
            String fromUserName = map.get("FromUserName");
            String msgType = map.get("MsgType");
            String content = map.get("Content");
            //判断请求是否事件类型 event
            if (MessageUtil.MESSAGE_EVENT.equals(msgType)) {
                String eventType = map.get("Event");
                //若是关注事件  subscribe
                if (MessageUtil.EVENT_SUB.equals(eventType)) {
                    Answer answer = answerService.getAnswerByAccountAccountID(toUserName);
                    Article article = new Article();
                    article.setTitle(answer.getTitle());
                    article.setDescription(answer.getDescription());
                    article.setPicUrl(answer.getPicUrl());
                    article.setUrl(answer.getUrl());
                    message = MessageUtil.newsMessage(toUserName, fromUserName, article);
                    System.out.println(message);
                }
            }
            out.print(message);
        } catch (Exception e) {
            e.printStackTrace();
            out.close();
        }

    }
}
